# 构建阶段
FROM node:20-alpine AS builder

WORKDIR /usr/src/app

# 设置环境变量
ENV NODE_OPTIONS=--max-old-space-size=4096
ENV YARN_CACHE_FOLDER=/tmp/.yarn-cache

# 复制依赖文件
COPY package.json ./

# 仅安装生产依赖
RUN yarn config set registry https://registry.npmmirror.com && \
    yarn install --production --frozen-lockfile && \
    yarn cache clean

# 复制构建所需文件
COPY .next ./.next
COPY public ./public
COPY next.config.ts .


# 生产环境阶段
FROM node:18.18-alpine

RUN apk add --no-cache tzdata
ENV TZ=Asia/Shanghai

WORKDIR /usr/src/app

# 从构建阶段复制必要文件
COPY --from=builder /usr/src/app/.next ./.next
COPY --from=builder /usr/src/app/public ./public
COPY --from=builder /usr/src/app/next.config.ts .
COPY --from=builder /usr/src/app/node_modules ./node_modules
COPY --from=builder /usr/src/app/package.json .

ENV NODE_ENV=production

EXPOSE 16660

CMD ["yarn", "start"]
